package com.dm.service;

import com.jacob.activeX.ActiveXComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static com.dm.util.KeyCode.KEY_CTRL;
import static com.dm.util.KeyCode.KEY_a;
import static com.enums.KeyboardKey.KEY_BACKSPACE;

@Slf4j
@Service
public class DmService {

    ActiveXComponent cm = new ActiveXComponent("dm.dmsoft");

    DMPlugin dm = DMPluginUtils.getDmPluginInstance();

    final Set<Integer> hindBindLock = new ConcurrentHashMap<Integer, Integer>().keySet();

    public int findWindow(String s, String name) {
        return dm.findWindow(s, name, cm);
    }

    /**
     * 截屏
     *
     * @param x
     * @param y
     * @param x1
     * @param y1
     * @param imgPath
     * @param hind    句柄
     * @return
     */
    public synchronized long capture(int x, int y, int x1, int y1, String imgPath, int hind) {
        bindWindow(hind);
        long capture = dm.capture(x, y, x1, y1, imgPath, cm);
        unBindWindow(hind);
        return capture;
    }

    private void unBindWindow(int hind) {
        long l = dm.unBindWindow(cm);
        log.info("取消绑定结果：{}", l);
        sleep(50);
        dm.setWindowState(hind, 9, cm);
    }

    private void bindWindow(int hind) {
        //获得锁
        long window = dm.bindWindow(hind, "normal", "normal", "normal", 1, cm);
        sleep(50);
        dm.setWindowState(hind, 8, cm);
        log.info("绑定窗口结果：{}", window);
    }

    private void sleep(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
        }
    }

    public synchronized long leftClick(int x, int y, int count, int hind) {
        bindWindow(hind);
        long result = dm.leftClick(x, y, cm, count);
        unBindWindow(hind);
        return result;
    }

    public String findPicE(int x, int y, int x1, int y1, String imgPath, String number, double sim, int dir, int hind) {
        bindWindow(hind);
        String result = dm.findPicE(x, y, x1, y1, imgPath, number, sim, dir, cm);
        unBindWindow(hind);
        return result;
    }

    public synchronized String jdlm(int hind, String url) {

        try {
            bindWindow(hind);
            return doJdlm(hind, url);
        } finally {
            unBindWindow(hind);
        }

    }

    private String doJdlm(int hind, String url) {
        dm.leftClick(922, 339, cm, 1);
        keyPressCtrlA();
        sleep(200);
        long l = dm.sendString(hind, url, cm);
        log.info("粘贴链接结果：{}", l);
        sleep(100);
        dm.leftClick(504, 548, cm, 1);
        int x = -1, y = -1;
        for (int i = 0; i < 20; i++) {
            String picE = dm.findPicE(760, 270, 958, 376, "D:/findWxImg/jdlm_fuzhi.bmp", "000000", 0.8, 0, cm);
            log.info("识别结果E:{}", picE);
            String[] ss = picE.split("\\|");
            boolean result = !"-1".equals(ss[0]);
            if (result) {
                x = Integer.parseInt(ss[1]);
                y = Integer.parseInt(ss[2]);
                break;
            }
            sleep(200);
        }
        if (x != -1) {
            dm.leftClick(x + 20, y + 20, cm, 1);
            sleep(200);
            dm.leftClick(652, 409, cm, 1);
            sleep(100);
            return dm.getClipboard(cm);
        }
        return null;
    }

    /**
     * 按一次ctrl+a
     */
    private void keyPressCtrlA() {
        dm.keyDown(KEY_CTRL, cm);
        sleep(50);
        dm.keyPress(KEY_a, cm);
        sleep(50);
        dm.keyUp(KEY_CTRL, cm);
        sleep(50);
        dm.keyPress(KEY_BACKSPACE.getAsciiCode(), cm);
        sleep(50);
    }
}
